import axios from 'axios'
import app from '@/app'

// 创建基础配置
const http = axios.create({
  baseURL: process.env.VUE_APP_SERVER_URL,
  timeout: 60000
});

// 响应拦截器
http.interceptors.response.use(
  // 正常响应
  response => {
    // 取出响应数据
    let data = response.data
    // 检测状态码
    if(data.error > 0) {
      app.ui.message('warning', 'server.' + data.message)
      return Promise.reject(data)
    }

    return data;
  },
  () => {
    app.ui.message('error', 'server.no')
    return Promise.reject()
  }
);

const server = {
  // 打开 Server
  open() {
    app.window.fileOpen(
      app.window.appPath() + '\\' + process.env.VUE_APP_EXTRA + '\\ShopayServer.exe'
    )
  },

  // 结束 Server
  close() {
    return http.get('/close')
  },

  printCashBox(name) {
    return http.get('/print/cashbox?name=' + name)
  },

  printTemplate(name, data) {
    return http.post('/print/template?name=' + name, data)
  },

  printPdf(name, file) {
    return http.post('/print/pdf?name=' + name, { file })
  }
}

export default server